package com.example.doer.reading;

import android.app.FragmentManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AbActivity {
    ImageView mTvTabBook;
    ImageView mTvTabDisscuss;
    ImageView mTvTabMessage;
    ImageView mTvTabMine;
    public static final String TAG ="tag";
    public static final int TAB_NUM = 4;
    //主页下面四个ImageView
    private ImageView[] mTabViews = new ImageView[TAB_NUM];
    //碎片管理
    private FragmentManager fragmentManager;
    //碎片对象列表
    private List<AbFragment> fragmentList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.activity_main);

        /*AbTitleBar abTitleBar=getTitleBar();
        abTitleBar.setTitleText("MainActivity");
        abTitleBar.setTitleBarBackground(R.drawable.top_bg2);
        abTitleBar.setTitleTextMargin(10, 0, 0, 0);*/
        initView();
    }


    public void initView(){
        mTvTabBook=(ImageView)findViewById(R.id.tv_tab_book);
        mTvTabDisscuss=(ImageView)findViewById(R.id.tv_tab_discuss);
        mTvTabMessage=(ImageView)findViewById(R.id.tv_tab_message);
        mTvTabMine=(ImageView)findViewById(R.id.tv_tab_mine);

        //获取FragmentManager
        fragmentManager=getFragmentManager();

        //初始化
        mTabViews[0]=mTvTabBook;
        mTabViews[1]=mTvTabDisscuss;
        mTabViews[2]=mTvTabMessage;
        mTabViews[3]=mTvTabMine;


        for (int i=0;i<mTabViews.length;i++){
            //添加4个fragment
            addFragment(i);
            final int j =i;

            //为每个imageView添加点击监听
            mTabViews[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showTab(j);
                }
            });
        }//end for

        //初始界面显示Tab 0
        showTab(0);
    }

    public void addFragment(int index){
        AbFragment fragment =null;
        switch (index) {
            case 0:
                fragment = new FragmentOne(getSupportFragmentManager());
                break;
            case 1:
                fragment = new FragmentTwo();
                //fragment = new FragmentTwo(getSupportFragmentManager());
                break;
            case 2:
                fragment = new FragmentThree(getSupportFragmentManager());
                break;
            case 3:
                fragment = new FragmentFour();
                break;
        }
        //碎片列表集合添加该imageView对应的碎片
        fragmentList.add(fragment);
        fragmentManager.beginTransaction().
                add(R.id.realtabcontent,fragment,String.valueOf(index)).
                commitAllowingStateLoss();
        fragmentManager.beginTransaction().
                hide(fragment).commitAllowingStateLoss();

        fragment.setUserVisibleHint(false);
    }

    private void hideAllFragments(AbFragment fragment) {
        for (int i = 0; i < TAB_NUM; i++) {
            AbFragment f = (AbFragment) fragmentManager.findFragmentByTag(String.valueOf(i));

            if (f != null && f.isAdded() && f != fragment) {
                fragmentManager.beginTransaction().hide(f).commitAllowingStateLoss();
            }
        }
    }

    public void showTab(int index){
        AbFragment targetFragment =fragmentList.get(index);
        hideAllFragments(targetFragment);
        fragmentManager.beginTransaction().show(targetFragment).commitAllowingStateLoss();
        targetFragment.setUserVisibleHint(true);
        for (int i = 0; i < TAB_NUM; i++) {
            if (index == i) {
                mTabViews[i].setBackgroundColor(getResources().getColor(R.color.navi_press));

            } else {
                mTabViews[i].setBackgroundColor(getResources().getColor(R.color.navi));

            }
        }
    }

    @Override
    protected void onResume(){
        int id  = getIntent().getIntExtra("flag",0);
        if(id==1){
            showTab(1);
        }
        super.onResume();
    }
}//end class
